package com.cc8w.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import org.springframework.stereotype.Service;

/**
 * 基于TCP协议的socket通讯(服务端)
 * 
 * 1.创建ServerSocket对象，绑定监听端口
 * 2.通过accept()方法监听客户端请求
 * 3.建立连接后,通过输入流读取客户端发送的请求信息
 * 4.通过输出流向客户端发送相应信息
 * 5.关闭相应资源
 * @author Administrator
 *
 */

@Service
public class ServerSocketService {
	public ServerSocketService() {}
	
	public ServerSocketService(int port) {
		
		
		try {
			//1.创建一个服务端Socket,即ServerSocket，指定端口，监听端口
			ServerSocket serverSocket = new ServerSocket(port);
			//2.调用accept()方法开始监听,等待客户端的链接
			System.out.println("服务端启动等待客户端链接...");
			Socket socket = serverSocket.accept();
			//3.一旦链接,获取输入流,获取客户端信息
			InputStream is = socket.getInputStream();//字节输入流
			InputStreamReader isr =  new InputStreamReader(is);//字节流转字符流
			BufferedReader br = new BufferedReader(isr);//字符流转缓冲字符流提高效率
			String info = null;
			while((info=br.readLine())!=null) {
				System.out.println("服务器提示-》客户端内容:"+info);
				
			}
			socket.shutdownInput();//关闭输入流
			//4.关闭相关资源
			br.close();
			isr.close();
			is.close();
			socket.close();
			serverSocket.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	
	//创建简单的socket服务端(只接受客户端消息输出下)
	public void createSimpleServer(int port) {
		
		
		try {
			//1.创建一个服务端Socket,即ServerSocket，指定端口，监听端口
			ServerSocket serverSocket = new ServerSocket(port);
			//2.调用accept()方法开始监听,等待客户端的链接
			System.out.println("服务端启动等待客户端链接...");
			Socket socket = serverSocket.accept();
			//3.一旦链接,获取输入流,获取客户端信息
			InputStream is = socket.getInputStream();//字节输入流
			InputStreamReader isr =  new InputStreamReader(is);//字节流转字符流
			BufferedReader br = new BufferedReader(isr);//字符流转缓冲字符流提高效率
			String info = null;
			while((info=br.readLine())!=null) {
				System.out.println("服务器提示-》客户端内容:"+info);
			}
			socket.shutdownInput();//关闭输入流
			
			
			//4.关闭相关资源
			//4.1输入流(相关)
			br.close();
			isr.close();
			is.close();

			
		
			socket.close();
			serverSocket.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
	}
	
	//创建socket服务端(可以接收客户端信息,也可以给客户端发信息)
	public void createServer(int port) {
		
		try {
			//1.创建socket服务端
			ServerSocket serverSocket = new ServerSocket(port);
			//2.阻塞监听客户端消息
			System.out.println("server:服务端启动等待客户端链接...");
			Socket socket = serverSocket.accept();
			//3.处理并显示客户端发来消息
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String info = null;
			while((info = br.readLine())!=null) {
				System.out.println("server: 我接收到了client发来的->"+info);
			}
			socket.shutdownInput();//关闭输入流

			//4.回应客户端消息
			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os);
			System.out.println("server: 给客户端回复-> 你好client,我server收到你信息");
			pw.write("你好client,我server收到你信息");
			pw.flush();
			socket.shutdownOutput();//关闭输出流
			
			//5.关闭资源
			//5.1输入流(相关)
			br.close();
			isr.close();
			is.close();	
			//5.2输出流(相关)
			pw.close();
			os.close();
			//5.3
			socket.close();
			serverSocket.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//创建socket服务端(批量 可以接收客户端信息,也可以给客户端发信息)
	public void createBatchServer(int port) {
		
		ServerSocket serverSocket  = null;
		try {
			//1.创建socket服务端
			serverSocket = new ServerSocket(port);
			while(true) {
				Socket socket = serverSocket.accept();
				ServerSocketThreadService ssts = new ServerSocketThreadService(socket);
				ssts.start();
			}
			
			
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
}
